<?php

namespace Keio\StudyBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Keio\StudyBundle\Entity\Monhoc;
use Keio\StudyBundle\Form\MonhocType;

/**
 * Monhoc controller.
 *
 */
class MonhocController extends Controller
{

    /**
     * Lists all Monhoc entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('KeioStudyBundle:Monhoc')->findAll();

        //set delete form cho mỗi element
        foreach ($entities as $entity){
        	$deleteForm = $this->createDeleteForm($entity->getId());
        	$entity->setDelform($deleteForm->createView());
        }

        $entity = new Monhoc();
        $form   = $this->createForm(new MonhocType(), $entity);

        return $this->render('KeioStudyBundle:Monhoc:index.html.twig', array(
            'entities' => $entities,
            'form'   => $form->createView(),
        ));
    }
    /**
     * Creates a new Monhoc entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity  = new Monhoc();
        $form = $this->createForm(new MonhocType(), $entity);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('keio_monhoc'));
        }
    }

    /**
     * Finds and displays a Monhoc entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('KeioStudyBundle:Monhoc')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Monhoc entity.');
        }

        return $this->render('KeioStudyBundle:Monhoc:show.html.twig', array(
            'entity'      => $entity,
             ));
    }

    /**
     * Displays a form to edit an existing Monhoc entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('KeioStudyBundle:Monhoc')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Monhoc entity.');
        }

        $editForm = $this->createForm(new MonhocType(), $entity);

        return $this->render('KeioStudyBundle:Monhoc:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
        ));
    }

    /**
     * Edits an existing Monhoc entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('KeioStudyBundle:Monhoc')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Monhoc entity.');
        }

        $editForm = $this->createForm(new MonhocType(), $entity);
        $editForm->bind($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('keio_monhoc'));
        }

        return $this->render('KeioStudyBundle:Monhoc:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
        ));
    }
    /**
     * Deletes a Monhoc entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('KeioStudyBundle:Monhoc')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Monhoc entity.');
            }

            //check if contraint
			$checkconstraint = $em->getRepository('KeioStudyBundle:Monhoc')->checkConstraintToDeleteMonhoc($id);
			if ($checkconstraint==1){ //having constraint
				$this->get('session')->getFlashBag()->add('msg', 'Delete failed because of constraint');
			}else{
				$em->remove($entity);
            	$em->flush();
			}
        }

        return $this->redirect($this->generateUrl('keio_monhoc'));
    }

    /**
     * Creates a form to delete a Monhoc entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
